package com.hddf.project.action;

import com.hddf.project.base.BaseAction;
import com.hddf.project.pojos.Comment;
import com.hddf.project.pojos.Project;
import com.hddf.project.pojos.User;
import com.hddf.project.service.CommentService;
import com.hddf.project.service.LogsService;
import com.hddf.project.service.ProjectService;
import com.hddf.project.util.O;

public class CommentAction extends BaseAction {

	private static final long serialVersionUID = -3897443529346150862L;

	private String content;
	private Integer cid;
	private Integer pid;
	private Project p;

	/**
	 * 添加批示
	 */
	public void addComment() {
		try {
			user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (O.ckSt(content) && O.ckNm(pid)) {
					CommentService cts = CommentService.getInService();
					Comment c = cts.addComment(user.getId(), pid, content);

					ls = LogsService.getInService();
					ls.addLog(user, "添加批注，批注ID为：" + c.getId() + "，批注内容为：" + content, (short) 1);
				} else {
					state = "error";
					msg = "请填写批示内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改批示
	 */
	public void updateComment() {
		try {
			user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (O.ckSt(content)) {
					CommentService cts = CommentService.getInService();
					cts.updateComment(cid, content);

					ls = LogsService.getInService();
					ls.addLog(user, "修改批注，批注ID为：" + cid + "，批注内容为：" + content, (short) 2);
				} else {
					state = "error";
					msg = "请填写批示内容";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除批示
	 */
	public void delComment() {
		try {
			user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CommentService cts = CommentService.getInService();
				cts.delComment(cid);

				ls = LogsService.getInService();
				ls.addLog(user, "删除批注，批注ID为：" + cid, (short) 3);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 跳转到批注页面
	 * 
	 * @return
	 */
	public String jumpListComment() {
		try {
			ProjectService ps = ProjectService.getInService();
			p = ps.getProjectById(pid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setSuccessPath("/page/browse_project/comment.jsp");
		return SUCCESS;
	}

	/**
	 * 根据项目ID获取批示数据
	 */
	public void listComment() {
		try {
			user = (User) getRequest().getAttribute("user");
			if (null != user) {
				CommentService cts = CommentService.getInService();
				msg = cts.listComment(pid);
				map.put("userId", user.getId());
				map.put("userName", user.getUsername());

				ls = LogsService.getInService();
				ls.addLog(user, "浏览批注，项目ID为：" + pid, (short) 4);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Integer getCid() {
		return cid;
	}

	public void setCid(Integer cid) {
		this.cid = cid;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public Project getP() {
		return p;
	}

	public void setP(Project p) {
		this.p = p;
	}

}
